package com.vshop.platform.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.vshop.framework.core.domain.R;
import com.vshop.framework.core.validation.AddGroup;
import com.vshop.framework.core.validation.EditGroup;
import com.vshop.framework.log.annotation.Log;
import com.vshop.framework.log.enums.BusinessType;
import com.vshop.platform.system.crud.service.SysDeptService;
import com.vshop.platform.system.domain.param.SysDeptParam;
import com.vshop.platform.system.domain.vo.SysDeptVo;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门表 控制层。
 *
 * @author vshop
 * @since 2024-11-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sysDept")
public class SysDeptController {

    private final SysDeptService sysDeptService;

    /**
     * 列表查询部门表。
     */
    @SaCheckPermission("sysDept:list")
    @GetMapping("/list")
    public R<List<SysDeptVo>> list(@ModelAttribute SysDeptParam param) {
        return R.ok(sysDeptService.list(param));
    }

    /**
     * 添加部门表。
     *
     * @param param 部门表
     */
    @Log(module = "部门管理", title = "添加部门", businessType = BusinessType.Insert)
    @SaCheckPermission("sysDept:save")
    @PostMapping("/save")
    public R<Void> save(@Validated(AddGroup.class)
                        @RequestBody SysDeptParam param) {
        return sysDeptService.save(param) ? R.ok() : R.fail();
    }


    /**
     * 根据主键更新部门表。
     *
     * @param param 部门表
     */
    @Log(module = "部门管理", title = "更新部门", businessType = BusinessType.Update)
    @SaCheckPermission("sysDept:update")
    @PostMapping("/update")
    public R<Void> update(@Validated(EditGroup.class)
                          @RequestBody SysDeptParam param) {
        return sysDeptService.update(param) ? R.ok() : R.fail();
    }


    /**
     * 根据主键删除部门表。
     *
     * @param id 主键
     */
    @Log(module = "部门管理", title = "删除部门", businessType = BusinessType.Delete)
    @SaCheckPermission("sysDept:remove")
    @PostMapping("/remove/{id}")
    public R<Void> remove(@NotNull(message = "主键不能为空")
                          @PathVariable Long id) {
        return sysDeptService.removeById(id) ? R.ok() : R.fail();
    }

    /**
     * 获取未被禁用的部门树列表
     */
    @GetMapping("/tree")
    public R<List<Tree<Long>>> tree() {
        SysDeptParam param = new SysDeptParam();
        return R.ok(sysDeptService.tree(param));
    }
}
